package cn.shiro.shiroservice.moudle.db.abstracts.other;

import cn.hutool.core.util.StrUtil;
import cn.shiro.shiroservice.auth.utils.ShiroUtils;
import cn.shiro.shiroservice.common.constant.ManaPoint;
import cn.shiro.shiroservice.moudle.db.pojo.entity.DatabaseUser;
import cn.shiro.shiroservice.moudle.db.pojo.enums.DataBaseTypeEnum;
import cn.shiro.shiroservice.moudle.db.service.DatabaseUserService;
import com.alibaba.druid.pool.DruidDataSource;
import com.mysql.cj.jdbc.Driver;

import javax.annotation.Resource;

import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * &#064;Time 2024 一月 星期一 17:30
 *
 * @author ShangGuan
 */
public abstract class AbstractCoreDatabaseController {



    @Resource
    private DatabaseUserService databaseUserService;


    protected final Map<Object, DruidDataSource> cacheMapDataSource = new ConcurrentHashMap<>();

    /**
     * 获取用户名
     *
     * @return {@link String}
     */

    protected String getUsername() {
        return ShiroUtils.getUsername();
    }


    protected String getName(String username) {
        if (StrUtil.isNotBlank(username)) {
            return AbstractCoreDatabaseController.class + username;
        }
        return AbstractCoreDatabaseController.class + "";
    }

    protected void loadDataSource() {
        loadDataSource(null);
    }

    /**
     * 加载数据源
     *
     * @param usernameDb 用户名数据库
     */

    protected void loadDataSource(String usernameDb) {
        try {
            DatabaseUser databaseInfo = databaseUserService.getDatabaseInfo(getUsername()).stream().findFirst().get();
            DruidDataSource druidDataSource = new DruidDataSource();
            druidDataSource.setUsername(databaseInfo.getUsername());
            druidDataSource.setPassword(databaseInfo.getPassword());
            druidDataSource.setDriver(new Driver());
            String url = getUrl(databaseInfo, usernameDb);
            druidDataSource.setUrl(url);
            cacheMapDataSource.put(getName(getUsername()), druidDataSource);
        } catch (SQLException | NullPointerException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 获取url
     *
     * @param databaseUser 数据库用户
     * @param usernameDb   用户名数据库
     * @return {@link String}
     */

    private String getUrl(DatabaseUser databaseUser, String usernameDb) {
        if (databaseUser == null) {
            throw new RuntimeException("null");
        }
        String databaseType = databaseUser.getDatabaseType();
        DataBaseTypeEnum dataBaseTypeEnum = DataBaseTypeEnum.valueOf(databaseType);
        String urlPrefix = dataBaseTypeEnum.getUrlPrefix();
        if (StrUtil.isNotBlank(usernameDb)) {
            return urlPrefix + databaseUser.getIpHost() + ":" + ManaPoint.MYSQL_PORT + "/" + usernameDb + "?" + databaseUser.getPort();
        }
        return urlPrefix + databaseUser.getIpHost() + ":" + ManaPoint.MYSQL_PORT + "/" + databaseUser.getDatabaseName() + "?" + databaseUser.getPort();

    }

}
